package gui.data.D3;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.PolyhedronCalculator;
import calcs.PyramidCalculator;

public class PyramidInputGui extends JPanel {
	JLabel volumeLbl, areaLbl, sAreaLbl, bAreaLbl, heightLbl, sHeightLbl, sideLbl;
	JTextField volumeInput, areaInput, bAreaInput, sAreaInput, heightInput, sHeightInput, sideInput;
	MyFrame myFrame;

	public PyramidInputGui(MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(8, 2));
		this.setPreferredSize(new Dimension(500, 500));
		volumeLbl = new JLabel("Volume = ");
		sAreaLbl = new JLabel("Side area = ");
		bAreaLbl = new JLabel("Bottom area = ");
		areaLbl = new JLabel("Total area = ");
		heightLbl = new JLabel("Height = ");
		sHeightLbl = new JLabel("Side height = ");
		sideLbl = new JLabel("Bottom edge length = ");
		volumeInput = new JTextField();
		sAreaInput = new JTextField();
		bAreaInput = new JTextField();
		areaInput = new JTextField();
		heightInput = new JTextField();
		sHeightInput = new JTextField();
		sideInput = new JTextField();
		this.add(sideLbl);
		this.add(sideInput);
		this.add(heightLbl);
		this.add(heightInput);
		this.add(volumeLbl);
		this.add(volumeInput);
		this.add(areaLbl);
		this.add(areaInput);
		this.add(bAreaLbl);
		this.add(bAreaInput);
		this.add(sAreaLbl);
		this.add(sAreaInput);
		this.add(sHeightLbl);
		this.add(sHeightInput);
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		this.add(new JLabel());
		this.add(calculate);
	}
	
	private void openResult() {
		
		boolean error = false;
		JTextField[] textFields = {volumeInput, areaInput, bAreaInput, sAreaInput, sHeightInput, heightInput, sideInput};
		Double[] values = new Double[7];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new PyramidCalculator(values[0], values[1], values[2], values[3], values[4], values[5], values[6], myFrame);
		}
	}
}
